//
//  VideoViewDetailViewController.m
//  cityStar
//
//  Created by joyban on 15-1-21.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "VideoViewDetailViewController.h"
#import "SGFocusImageFrame.h"
#import "SGFocusImageItem.h"
#import "UIColor+FlatUI.h"

#import "MakefirendsdetailCell.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"

NSString *const VideodetailCellID = @"videodetailCellID";

@interface VideoViewDetailViewController ()<SGFocusImageFrameDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleTable;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *homenubLabel;
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *sourceLabel;
@property (weak, nonatomic) IBOutlet UILabel *categoryLabel;

@property (strong, nonatomic) IBOutlet UIView *heardView;
@property (weak, nonatomic) IBOutlet UIView *scrolerlView;
@property (weak, nonatomic) IBOutlet UITableView *VideoDetailView;
@property (strong,nonatomic) SGFocusImageFrame *heardTableView;
@property (weak, nonatomic) IBOutlet UIButton *Consumptionbtn;
@property (weak, nonatomic) IBOutlet UILabel *payType;

@end

@implementation VideoViewDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"租售详情";
    [self addBackButton];
    [self setUpTableViews];
    [self setHeardTableView];
    [self setdataHeardView];
    if([self.withchType isEqualToString:@"cityService"])
    {
        [self.Consumptionbtn setTitle:@"消费积分查看联系方式" forState:UIControlStateNormal];
    }else{
        [self.Consumptionbtn setTitle:@"信息作废" forState:UIControlStateNormal];
        [self.Consumptionbtn setTitle:@"信息作废" forState:UIControlStateHighlighted];

        [self.Consumptionbtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    }
    
}

-(void)setdataHeardView
{
    self.titleTable.text = self.videoModel.title;
    self.addressLabel.text = self.videoModel.district_name;
    NSString *room = [NSString stringWithFormat:@"%@室",self.videoModel.room];
    NSString *hall = [NSString stringWithFormat:@"%@厅",self.videoModel.hall];
    NSString *bathroom = [NSString stringWithFormat:@"%@卫",self.videoModel.bathroom];
    NSString *area = [NSString stringWithFormat:@"%@平方",self.videoModel.area];
    self.homenubLabel.text = [NSString stringWithFormat:@"%@%@%@",room,hall,bathroom];
    self.areaLabel.text = area;
    
    NSInteger typestring = [[NSString stringWithFormat:@"%@",self.videoModel.category] integerValue];
    switch (typestring) {
        case 1:
        {
            self.categoryLabel.text = @"整租";
            NSString *stringMoney = [NSString stringWithFormat:@"%@",self.videoModel.money];
            self.priceLabel.text = [NSString stringWithFormat:@"%@元/月",stringMoney];
        }

            break;
        case 2:
        {
            NSString *stringMoney = [NSString stringWithFormat:@"%@",self.videoModel.money];
            self.categoryLabel.text = @"合租";
            self.priceLabel.text = [NSString stringWithFormat:@"%@元/月",stringMoney];
        }
            break;
        case 3:
        {
            NSString *stringMoney = [NSString stringWithFormat:@"%@",self.videoModel.money];
            self.categoryLabel.text = @"出售";
            self.priceLabel.text = [NSString stringWithFormat:@"%@元/平方",stringMoney];
        }
            break;
        default:
            break;
    }
    NSInteger pattypestr = [[NSString stringWithFormat:@"%@",self.videoModel.pay_type] integerValue];
    switch (pattypestr) {
        case 12:
        {
            self.payType.text = @"付一押二";
        }
            break;
        case 31:
        {
            self.payType.text = @"付三押一";
        }
            break;
        case 60:
        {
            self.payType.text = @"半年付不押";
        }
            break;
        case 00:
        {
            self.payType.text = @"年付不压";
        }
            break;
        default:
            break;
    }
    NSInteger sourcestr = [[NSString stringWithFormat:@"%@",self.videoModel.source] integerValue];
    switch (sourcestr) {
        case 1:
            self.sourceLabel.text = @"业主";
            self.sourceLabel.backgroundColor = [UIColor colorFromHexCode:@"ff9a66"];
            break;
        case 2:
            self.sourceLabel.text = @"中介";
            self.sourceLabel.backgroundColor = [UIColor colorFromHexCode:@"0068b7"];
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setHeardTableView
{
    NSMutableArray *tempArray = [NSMutableArray array];
    
    __block NSInteger length;
    NSLog(@"%@",self.videoModel.image);
    if (self.videoModel.image.count > 0) {
        [self.videoModel.image enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
            NSString *strimagepath = [NSString stringWithFormat:@"http://api.icszx.com/image/c/id/%@/w/640/h/300/m/1",self.videoModel.image[idx]];
            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:strimagepath,@"imagePath",nil];
            length = tempArray.count+1;
            [tempArray addObject:dict];
        }];
    }else{
        tempArray = [NSMutableArray array];
        length = 1;
        for (int i = 0 ; i < length; i++)
        {
            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"banner",@"imagePath",nil];
            [tempArray addObject:dict];
        }
    }
    //添加最后一张图 用于循环
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:length+2];
    if (length > 1)
    {
        NSDictionary *dict = [tempArray objectAtIndex:length-1];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:-1] ;
        [itemArray addObject:item];
    }
    for (int i = 0; i < length; i++)
    {
        NSDictionary *dict = [tempArray objectAtIndex:i];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:i];
        [itemArray addObject:item];
    }
    //添加第一张图 用于循环
    if (length >1)
    {
        NSDictionary *dict = [tempArray objectAtIndex:0];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:length];
        [itemArray addObject:item];
    }
    self.heardTableView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) delegate:self imageItems:itemArray isAuto:NO];
    [self.heardTableView scrollToIndex:2];
    [self.scrolerlView addSubview: self.heardTableView];
    self.VideoDetailView.tableHeaderView = self.heardView;
}

-(void)foucusImageFrame:(SGFocusImageFrame *)imageFrame didSelectItem:(SGFocusImageItem *)item ImagePage:(int)pageIndex
{
    
}

-(void)setUpTableViews
{
    [self.VideoDetailView registerNib:[MakefirendsdetailCell nib] forCellReuseIdentifier:VideodetailCellID];
}

#pragma mark - Actions
-(IBAction)getContact:(id)sender
{
    if([self.withchType isEqualToString:@"cityService"])
    {
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%i",KinterFace_PublishContact,self.videoModel._id,[self.videoModel.category intValue]];
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            if(![self.Consumptionbtn.titleLabel.text isEqualToString:@"消费积分查看联系方式"])
            {
                NSString *string = [NSString stringWithFormat:@"%@",response[@"value"][@"contacts"][@"phone"]];
                NSString *telStr=[@"tel://" stringByAppendingString:string];
                NSURL *url=[NSURL URLWithString:telStr];
                [[UIApplication sharedApplication]openURL:url];
            }else{
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否消耗积分查看联系方式" message:nil delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否",nil];
                [alertView show];
            }
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
    }else{
        RIButtonItem *itemCancel = [RIButtonItem itemWithLabel:@"取消"];
        RIButtonItem *itemSubMit = [RIButtonItem itemWithLabel:@"确定" action:^{
            NSString *postUrl = [NSString stringWithFormat:@"%@%@",KinterFace_PublishInvalid,self.videoModel._id];
            [HttpRequestManager deleteMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
                if (success) {
                    [SVProgressHUD showSuccessWithStatus:@"信息作废成功"];
                }else{
                    [SVProgressHUD showErrorWithStatus:@"信息作废失败"];
                }
            }];
        }];
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否作废该信息" message:nil cancelButtonItem:itemCancel otherButtonItems:itemSubMit, nil];
        [alertView show];
    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (buttonIndex == 0) {
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%i",KinterFace_PublishContact,self.videoModel._id,[self.videoModel.category intValue]];
        [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                DLog(@"%@",response);
                NSString *string = [NSString stringWithFormat:@"%@",response[@"value"][@"contacts"][@"phone"]];

                NSString *title = [NSString stringWithFormat:@"%@:%@",self.videoModel.contact,string];
                [self.Consumptionbtn setTitle:title forState:UIControlStateNormal];
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }
}


#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ss = [NSString stringWithFormat:@"%@",self.videoModel.desc];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 25)];
    label.font = [UIFont systemFontOfSize:14];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setNumberOfLines:0];
    NSString *_Detail = [NSString stringWithFormat:@"%@",ss];
    label.text = _Detail;
    [label sizeToFit];
    float labelheight = label.frame.size.height;
    return [MakefirendsdetailCell heightOfTableCell] - (80 - labelheight);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel * label = [[UILabel alloc] init] ;
    label.frame = CGRectMake(15, 0, 200, 25);
    label.backgroundColor = [UIColor clearColor];
    label.font=[UIFont systemFontOfSize:14];
    label.textColor = [UIColor colorFromHexCode:@"949494"];
    label.text = @"详细描述";
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"FFF3E2"]];
    [sectionView addSubview:label];
    return sectionView;
    
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MakefirendsdetailCell *VideodetailCell = [tableView dequeueReusableCellWithIdentifier:VideodetailCellID];
    VideodetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
    VideodetailCell.contentLabel.text = self.videoModel.desc;
    return VideodetailCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
